package com.otitan.la.forest.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.otitan.la.forest.system.dto.OrganizationDTO;
import com.otitan.la.forest.system.entity.OrganizationEntity;
import com.otitan.la.forest.system.service.OrganizationService;
import com.otitan.la.forest.system.vo.OrganizationEntityVO;
import com.otitan.la.forest.system.vo.OrganizationStationVO;
import com.otitan.la.forest.system.vo.OrganizationTreeVO;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * 组织机构表
 *
 * @author jiacs
 * @email xxx
 * @date 2020-09-15 10:34:19
 */
@Slf4j
@Api(tags = "组织机构表API")
@RestController
@RequestMapping("/organization")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;


    @ApiOperation(value = "新增")
    @PostMapping()
    public DataResult add(@Valid @RequestBody OrganizationEntity organization, BindingResult bindingResult) {
        return organizationService.add(organization, bindingResult);
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public DataResult batchDelete(@RequestBody @ApiParam(value = "ID集合") List<String> ids) {
        return organizationService.batchDelete(ids);
    }
    @ApiOperation(value = "删除")
    @DeleteMapping("{id}")
    public DataResult delete(@PathVariable("id") String id) {
        List<String> ids = new ArrayList<String>() {{
            add(id);
        }};
        return organizationService.batchDelete(ids);
    }

    @ApiOperation(value = "更新")
    @PutMapping
    public DataResult editor(@Valid @RequestBody OrganizationEntity organization, BindingResult bindingResult) {
        return organizationService.editor(organization, bindingResult);
    }

    @ApiOperation(value = "列表查询（分页）")
    @PostMapping("/{page}/{size}")
    public DataResult<IPage<OrganizationEntityVO>> findPageData(@PathVariable("page") long page,
                                                                @PathVariable("size") long size,
                                                                @RequestBody OrganizationDTO organization) {
        return organizationService.findPageData(page, size, organization);
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("/{id}")
    public DataResult<OrganizationEntityVO> findOne(@PathVariable("id") String id) {
        return organizationService.findOne(id);
    }


    @ApiOperation(value = "获取组织机构列表树")
    @GetMapping("findOrgTree")
    public DataResult<List<OrganizationTreeVO>> getOrganizationTree() {
        List<OrganizationTreeVO> listTree=organizationService.getOrganizationTree();
        return DataResult.success(listTree);
    }

    @ApiOperation(value = "获取所有管理站")
    @GetMapping("findOrgMap")
    public DataResult< List<OrganizationStationVO>> findOrgMap() {
        List<OrganizationStationVO> listTree=organizationService.findOrgMap();
        return DataResult.success(listTree);
    }

    @ApiOperation(value = "通过组织机构名查询地区编码和id")
    @GetMapping("findCodeAndIdByOrg")
    public DataResult findCodeAndIdByOrg(String orgName){
        return organizationService.findCodeAndIdByOrg(orgName);
    }

    @ApiOperation(value = "通过id查询组织机构名")
    @GetMapping("findOrgById")
    public DataResult findOrgById(String id){
        return organizationService.findOrgById(id);
    }
}
